<?php 
	@session_start();
	error_reporting(E_ERROR);  
    ini_set("display_errors","Off");  
	//require_once('../user/wxbrower.php');
	require_once("../includes/public.inc.php");
	$s = isset($_GET['s'])?$_GET['s']:'';
	//$username = isset($_SESSION['username'])?$_SESSION['username']:'';
	$sysconfig=mysql_fetch_array(mysql_query("select * from ".DBQIAN."sys_config limit 1"));
	/*if (!$s)
	{
		echo "该二维码为非法防伪码，购买的产品请谨慎使用!";exit;
		//echo "<script type='text/javascript'>alert('该二维码为非法防伪码，请谨慎使用!');return false;</script>";exit;
	}
	if (!isset($_GET['code']))
	{
		$uwxcode=$_GET['uwxcode'];
		$locaurl=urlencode(WEBNAME."main/zhengpu.php?s=".$s."&uwxcode=".$uwxcode);//网址解析
		$urls="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".trim($sysconfig['cappid'])."&redirect_uri=".$locaurl."&response_type=code&scope=snsapi_base#wechat_redirect";
		header("Location:".$urls);exit;
	}
	$code = $_GET['code'];//获取code
	$weixin =  file_get_contents("https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$sysconfig['cappid']."&secret=".$sysconfig['cappsecret']."&code=".$code."&grant_type=authorization_code");//通过code换取网页授权access_token
	$jsondecode = json_decode($weixin); //对JSON格式的字符串进行编码
	$array = get_object_vars($jsondecode);//转换成数组
	$openid = $array['openid'];//输出openid
	*/
	//获取用户微信openid
	$openid = 'oYqwzuFX-ue9vd9Ws8d1cl3xEo90';	//使用单一入口
	
	function getAccessToken()
	{
		$sysconfig=mysql_fetch_array(mysql_query("select * from ".DBQIAN."sys_config limit 1"));
		$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$sysconfig['cappid']."&secret=".$sysconfig['cappsecret'];
		$access = get_object_vars(json_decode(file_get_contents($url)));
		$access_token = $access['access_token'];
		 /*if ($sysconfig['ctokentime'] < time()) {
	      // 如果是企业号用以下URL获取access_token
	      // $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$this->appId&corpsecret=$this->appSecret";
	      $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$sysconfig['cappid']."&secret=".$sysconfig['cappsecret'];
	      $res = json_decode(curlGet($url));
	      print_r($res);exit;
	      $access_token = $res->access_token;
	      if ($access_token) {
	        $ctokentime = time() + 7000;
	        $ctoken = $access_token;
			mysql_query("update ".DBQIAN."sys_config set ctoken='$ctoken',
						  ctokentime='$ctokentime', where id=1");
	        //$fp = fopen("access_token.json", "w");
	       // fwrite($fp, json_encode($data));
	        //fclose($fp);
	      }
	    } else {
	      $access_token = $sysconfig['ctoken'];
	    }*/
	    return $access_token;
	}
	
	/*function curlGet($url)
	{
		$curl = curl_init();
	    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
	    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
	    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
	    curl_setopt($curl, CURLOPT_URL, $url);
	
	    $res = curl_exec($curl);
	    curl_close($curl);
	
	    return $res;
	}*/
	
	$access_token=getAccessToken();//不需要关注公众号，暂时不需要使用这段代码
	$subscribe_msg = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$access_token."&openid=".$openid;
	$subscribe = get_object_vars(json_decode(file_get_contents($subscribe_msg)));
	$zyxx = $subscribe['subscribe'];
	//print_r($subscribe);exit;
	if($zyxx !== 1)
	{
		header("Location: ../user/guanzhu.php");exit;
	}
	
	//判断是否关注
	//require_once("../includes/conn.php");
	//$s=isset($_GET['s'])?$_GET['s']:'';
	//$openid = isset($_GET['openid'])?$_GET['openid']:'';//echo $openid;exit;
	//$username = isset($_SESSION['username'])?$_SESSION['username']:'';
	//不需要判断是否关注，主需要判断这个吗是否是存在而且是没被领取的
	//print_r($_SESSION['username']);exit;
	if ($openid)
	{
		$openusers =  mysql_fetch_array(mysql_query("select * from ".DBQIAN."users where openid='".$openid."' order by id desc limit 1"));
		//print_r($openusers);exit;
		if ($openusers)
		{
			//进入页面不做其他动作
			$_SESSION['nickname'] = $subscribe['nickname'];
			$_SESSION['openid'] = $subscribe['openid'];
		}
		else //记录信息
		{
			$intouser = mysql_query("insert into ".DBQIAN."users (username,name,sex,country,province,city,credit,createtime,openid,headimg_url,last_login,subscribe) values ('".$subscribe['nickname']."','".$subscribe['nickname']."','".$subscribe['sex']."','".$subscribe['country']."','".$subscribe['province']."','".$subscribe['city']."',0,'".date('y-m-d H:i:s',time())."','".$subscribe['openid']."','".$subscribe['headimgurl']."','".$subscribe['subscribe_time']."',".$subscribe['subscribe'].")");//增加次数
			$_SESSION['nickname'] = $subscribe['nickname'];
			$_SESSION['openid'] = $subscribe['openid'];
		}
		$fwm = mysql_fetch_array(mysql_query("select id,fwm,time,usercredit,times,userstatus from ".DBQIAN."fwm where fwm='".trim($s)."' order by id desc limit 1"));//未使用,未领取奖品的
		mysql_query("update ".DBQIAN."fwm set times=times+1 where fwm='".trim($s)."'"); //记录扫码次数
	}
	else 
	{
		echo "<script>alert('非法进入，请检查提交的数据是否正确!!');</script>";exit;
	}
?>

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
<meta name="format-detection" content="telephone=no">
<title>摇红包</title>

<link rel="stylesheet" href="Public/css/csshake.min.css">
<link rel="stylesheet" href="Public/css/style.css">
<script src="Public/js/zepto.min.js"></script>
<script src="Public/js/red.js"></script>
</head>
<body bgcolor="orange">
<!-- 红包 -->
<div class="red"><!-- shake-chunk -->
	<?if ($fwm):?>
		<?if ($fwm['userstatus']==0):?>
		    <span style="background-image: url(Public/img/red-w.png);"></span>
		    <button class="redbutton" type="领取红包">拆红包</button>
		    <div class="red-jg" style="width:95%;margin-left:auto;margin-right:auto;text-align:center;">
		        <h1>恭喜您！</h1>
		        <h5>获得<?=$fwm['usercredit']?>的红包</h5>
		        <h5>该码被扫<?=$fwm['times']?>次</h5>
		    </div>
		<?else:?>
			<span style="background-image: url(Public/img/red-y.png);"></span>
			<div class="red-jg" style="display:block;width:95%;margin-left:auto;margin-right:auto;text-align:center;">
		        <h1>该码无效！</h1>
		        <h5>红包已被领取！</h5>
		        <h5>该码被扫<?=$fwm['times']?>次</h5>
		    </div>
		<?endif;?>
    <?else:?>
    <span style="background-image: url(Public/img/red-y.png);"></span>
    <div class="red-jg" style="display:block;width:95%;margin-left:auto;margin-right:auto;text-align:center;">
        <h1>该码无效！</h1>
        <h5>为假冒产品！</h5>
        <h5>谨慎使用！</h5>
    </div>
    <?endif;?>
</div>
<!-- End 红包 -->
<!-- 按钮 -->
<div class="t-btn" style="display:none;">
    <button onclick="location='<?=WEBNAME?>main/lj.php?sk=1445313033&sh=43920&s=<?=$fwm['fwm']?>&type=user&openid=<?=$openid?>'">立即领取</button>
</div>
<!-- End 按钮 -->
<div style="text-align:center;margin:50px 0; font:normal 14px/24px 'MicroSoft YaHei';">
<p>旭发防伪&copy;2016 版权所有</p>
</div>
</body>
</html>